package jian.thread.newbie;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 测试线程池<br>
 * 
 * @author Jian
 * @date 2013-06-19
 * 
 */
public class TestThreadPool {

	public static void main(String[] args) {
		ExecutorService threadPool = Executors.newFixedThreadPool(3);
		// ExecutorService threadPool = Executors.newCachedThreadPool();
		// ExecutorService threadPool = Executors.newSingleThreadExecutor();
		// Task task = new Task(0);
		for (int i = 1; i <= 4; i++) {

			final int taskId = i;
			threadPool.execute(new Runnable() {
				@Override
				public void run() {
					for (int j = 1; j <= 3; j++) {
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						System.out.println("TaskID : " + taskId + " 运行第[" + j
								+ "]次, 当前线程 ==>"
								+ Thread.currentThread().getName());
					}
				}
			});

		}

		threadPool.shutdown();
	}
}

class Task implements Runnable {
	private volatile int id;

	public synchronized int getId() {
		return id;
	}

	public synchronized void setId(int id) {
		this.id = id;
	}

	public Task(int id) {
		this.id = id;
	}

	@Override
	public void run() {
		for (int j = 1; j <= 3; j++) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("TaskID : " + id + " 运行第[" + j + "]次, 当前线程 ==>"
					+ Thread.currentThread().getName());
		}
	}

}